﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace MachineBuildingSystem
{
    public partial class FormLogin : Form
    {
        //登陆过的用户
        private string m_HaveLoginNameStr = "";

        private string m_LoginName = "";
        public string LoginName
        {
            get { return m_LoginName; }
        }
        private string m_Password = "";
        public string Password
        {
            get { return m_Password; }
        }

        private string m_EmployeeName = "";
        public string EmployeeName
        {
            get { return m_EmployeeName; }
        }

        private List<long> m_ActionList = new List<long> ();
        public List<long> ActionList
        {
            get { return m_ActionList; }
        }

        public FormLogin()
        {
            InitializeComponent();
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {
            m_HaveLoginNameStr = ConfigurationManager.AppSettings["UserNames"];

            foreach (string name in m_HaveLoginNameStr.Split(','))
                cb_UserName.Items.Add(name);
            cb_UserName.SelectedIndex = 0;
        }

        private void txt_Password_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)//回车登录
            {
                login();
            }
        }
        private void btn_login_Click(object sender, EventArgs e)//按键登录
        {
            login();
        }

        private void login()
        {
            string name = cb_UserName.Text.Trim();
            string password = txt_Password.Text.Trim();

            try
            {
                //json登录
                if (!CommonHelper.JsonLogin(name, password, ref m_EmployeeName))
                {
                    MessageBox.Show("Json登录失败,详见日志");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
                return;
            }
          
            //获取权限
            string sql = "select n.ACTION_ID from HS_SECU_USER m left join " 
                + "(select t.USER_ID, s.ACTION_ID from HS_SECU_ROLE_ACTION s " 
                + " left join  HS_SECU_ROLE_USER t on s.ROLE_ID = t.ROLE_ID) n " 
                + " on m.ID = n.USER_ID where m.USERNAME = '" + name + "'";
            try
            {
                DataTable dt = DB2Helper.CreateInstance().DoDB2Select(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (string.IsNullOrEmpty(dr["ACTION_ID"].ToString()))
                            continue;
                        long actionId = long.Parse(dr["ACTION_ID"].ToString());
                        if (!m_ActionList.Contains(actionId))
                            m_ActionList.Add(actionId);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取权限异常：" + ex.Message + "sql:" + sql);
                if (MessageBox.Show("获取该用户权限数据异常,是否继续？", "", MessageBoxButtons.YesNo) != DialogResult.Yes)
                    return;
            }
          
            if (cb_savename.Checked)
            {
                if (!m_HaveLoginNameStr.Split(',').Contains(name))
                {
                    m_HaveLoginNameStr += "," + name;
                    Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    cfa.AppSettings.Settings["UserNames"].Value = m_HaveLoginNameStr;
                    cfa.Save();
                    ConfigurationManager.RefreshSection("appSettings");
                }
            }
            m_LoginName = name;
            m_Password = password;
            this.DialogResult = DialogResult.OK;
        }

        private void btn_cancel_Click(object sender, EventArgs e)//取消登录
        {
            this.Close();
        }
    }
}
